define([
    'require',
    'jquery',
    'header',
    'dateRange',
    'selector',
    'modal',
    'tooltip',
    'nav',
    'plat_public',
    'message',
    'bigwe_utils',
    'pagination',
    'artTemplate',
    'api',
    'bigwe_component',
], function(require, $) {
    var message = require('message'),
        definedCallback = require('plat_public'),
        pickerDateRange = require('dateRange'),
        util = require('bigwe_utils'),
        pagination = require('pagination'),
        at = require('artTemplate'),
        thisAPI = require('api'),
        delete_list = require('bigwe_component');

    /* ====== * 平台,公众号 * ====== */
    definedCallback.init({
        platform: ['weixin', 'sina', 'toutiao'],
        autoRefresh: false,
        callback: acount_info
    });

    $('.selectpicker').selectpicker();

    var cont = $('.main_left');
    var acount = {};
    var start_date = new Date(new Date() - 86400000 * 14).format('yyyy-MM-dd'),
        end_date = new Date().format('yyyy-MM-dd');
    var param = {
        weixin: {
            url: '/sending_log/log_list_api',
            data: {
                p: 1,
                l: 10,
                time: [start_date, end_date],
                mt: '',
                ms: '',
                key: '',
                appid: ''
            },
            text: '全部公众号',
            timeFilter: true,
        },
        sina: {
            url: '/sina/status_list',
            data: {
                p: 1,
                time: [start_date, end_date]
            },
            text: '全部微博号',
            timeFilter: true,
        },
        toutiao: {
            url: '/toutiao/tt_art_list',
            data: {
                p: 1,
                time: [start_date, end_date]
            },
            text: '全部头条号',
            timeFilter: true,
        },
        pag: true,
    };

    function acount_info(data, platform, t) {
        if (data) {
            acount.info = data;
            acount.pt = platform;
            param[platform].data.p = 1;
            param.pag = true;
            $('.public_choose').removeClass('hide');
            if (!t || data.include == 'all') {
                delete param[platform].data.appid || param[platform].data.uid;
                if (!t) {
                    var key = (platform == 'weixin') ? param[platform].data.key : param[platform].data.select;
                    $('input.search_input').val(key || '');
                }
                cont.find('.account_info').empty().append('<p style="line-height:0px;text-align: center;margin: 24px;">' + param[platform].text + '</p>');
                if (platform == 'weixin') {
                    weixin_msg.init();
                } else if (platform == 'sina') {
                    weibo_list.init();
                } else {
                    tout_list.init();
                }
            } else {
                $('.account_info').html(at('user_default_account_html', data));
                (platform == 'weixin') ? (param[platform].data.appid = acount.info.app_id) : (param[platform].data.uid = acount.info.uid);
                weixin_msg._doSearchMsg();
            }
            if (platform == 'weixin') {
                cont.find('.weixin_list').show();
                cont.find('.weibo_list,.toutiao_list').hide();
            } else if (platform == 'sina') {
                cont.find('.weibo_list').show();
                cont.find('.weixin_list,.toutiao_list').hide();
            } else {
                cont.find('.toutiao_list').show();
                cont.find('.weibo_list,.weixin_list').hide();
            }
        }
    }

    var weixin_msg = {
        init: function() {
            if (param.weixin.timeFilter) {
                this.dateFilter();
                param.weixin.timeFilter = false;
            }
            this._doSearchMsg();
        },
        _doSearchMsg: function() {
            $('.weixin_msglist').empty();
            $("#article_paging").hide();
            var $this = this;
            message.show('正在加载...');
            $.ajax({
                type: 'post',
                url: param.weixin.url,
                data: param.weixin.data,
                success: function(Json) {
                    var Json = $.parseJSON(Json);
                    var Json_arr = Json.data.list;
                    var page = Json.data.page;
                    var html = '';
                    if (Json_arr.length) {
                        for (key in Json_arr) {
                            var obj = Json_arr[key];
                            //信息头
                            html += HtmlBuild.getMsgHeadHtml(obj);
                            //图片
                            var contents = obj.js_content;
                            var contents = $.parseJSON(contents);
                            html += HtmlBuild.getMsgImgHtml(contents.cover_img_url, obj, contents.cover_img_url);

                            //标题
                            var titles = contents.texts;
                            html += HtmlBuild.getMsgTitleHtml(titles, obj.msg_type_cd, contents.cover_img_url);
                            //发送类型判断
                            html += HtmlBuild.getMsgTypeHtml(obj.msg_type_cd);
                            //发送对象
                            var send_to = $.parseJSON(obj.js_app);
                            html += HtmlBuild.getSendHtml(send_to);
                            //发送状态判断
                            html += HtmlBuild.getMsgStatusHtml(obj.js_result, obj.msg_status_cd);
                            //操作
                            html += '<td class="btn-operate">';
                            if (obj.is_timer == 1 && (obj.msg_status_cd == 2 || obj.msg_status_cd == 3)) {
                                html += '<a href="javascript:void(0)" class="iconfont icon-pen icon-Writing" title="编辑"></a>';
                            }

                            if (obj.msg_status_cd != 4 ) {
                                html += '<a href="javascript:void(0);"  delete-event class="glyphicon glyphicon-trash icon-delete" title="删除" data-id="' + send_to.app_id + '"></a>';
                            }

                            html += '</td>' + '</tr>' + '</table>' + '</div>' + '</div>';
                        }
                    } else {
                        html += '<div class="no-content"><span class="glyphicon glyphicon-flash"></span>暂无群发消息记录</div>';
                    }
                    $('.weixin_msglist').append(html);
                    if (param.pag) {
                        $this.page(page.total_pages);
                    }
                    if (page.total_pages > 1) {
                        $("#article_paging").show();
                    }
                },
                complete: function() {
                    $('.icon-Writing, .icon-delete', '.msg-content').tooltip({
                        trigger: 'hover',
                        container: 'body'
                    });
                    message.hide();
                }
            });
        },
        //按时间区间筛选
        dateFilter: function() {
            var $this = this;
            var dateRange = new pickerDateRange('date-fromto-text', {
                isTodayValid: true,
                startDate: param[acount.pt].data.time[0],
                endDate: param[acount.pt].data.time[1],
                isSingleDay: false, // 是否单选天
                shortOpr: false, // 是否只选一天
                defaultText: ' 至 ',
                inputTrigger: 'date-fromto',
                theme: 'ta',
                stopToday: false,
                success: function(obj) {
                    param.weixin.data.time = [obj.startDate, obj.endDate];
                    param.weixin.data.p = 1;
                    param.pag = true;
                    $this._doSearchMsg();
                }
            });
            return this;
        },
        //分页
        page: function(p) {
            var current_page = param.weixin.data.p - 1;
            $("#article_paging").pagination(p * 10, {
                num_edge_entries: 0,
                items_per_page: 10,
                current_page: current_page,
                prev_text: '上一页',
                next_text: '下一页',
                callback: function(page_index, jq) {
                    if (param.pag) {
                        param.pag = false;
                        return;
                    } else {
                        param.weixin.data.p = page_index + 1;
                        weixin_msg._doSearchMsg();
                    }
                }
            });
        },
    };
    var weibo_list = {
        init: function() {
            if (param.sina.timeFilter) {
                this.dateFilter();
                param.sina.timeFilter = false;
            }
            this.infor_list();
        },
        //微博消息列表
        infor_list: function() {
            $('.weibo_msglist').empty();
            $("#weibo_paging").hide();
            var $this = this;
            message.show('正在加载...');
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: param.sina.url,
                data: param.sina.data,
                success: function(data) {
                    var arguments = data.data[0];
                    var totalPage = data.data[1];
                    var _html = '';
                    if (totalPage == 0) {
                        _html = '<div class="no-content"><span class="glyphicon glyphicon-flash"></span>暂无群发消息记录</div>';
                    } else {
                        for (var i in arguments) {
                            var time_type = arguments[i].time_type == 1 ? '立即发送' : '定时发送',
                                visible = arguments[i].user,
                                sina_type,
                                type,
                                biaot_wz,
                                send_type;

                            if (arguments[i].msgtype == 'text') {
                                type = '文字'
                                biaot_wz = '<div class="text">' + arguments[i].text + '</div>';
                            } else if (arguments[i].msgtype == 'articles') {
                                type = '图文'
                                var tuwen = '';
                                var tuwen_num;
                                if (arguments[i].art_url.length > 1) {
                                    for (var j in arguments[i].art_url) {
                                        tuwen += '<li>' + (Number(j) + 1) + '、' + '<a href="' + arguments[i].art_url[j] + '" target="_blank" title="' + arguments[i].text[j] + '">' + arguments[i].text[j] + '</a></li>'
                                    }
                                } else {
                                    tuwen += '<li><a href="' + arguments[i].art_url[0] + '" target="_blank" title="' + arguments[i].text[0] + '">' + arguments[i].text[0] + '</a></li>'
                                }

                                biaot_wz = tuwen;
                            } else {
                                type = '图片'
                                biaot_wz = '<li>' + arguments[i].text + '</li>';
                            }
                            if (arguments[i].send_type == 1) {
                                send_type = '已删除'
                            } else if (arguments[i].send_type == 2) {
                                send_type = '发送成功'
                            } else if (arguments[i].send_type == 3) {
                                send_type = '发送失败'
                            } else {
                                send_type = '待发送'
                            }
                            if (arguments[i].sina_type == 'status') {
                                sina_type = '【发微博】'
                            } else {
                                sina_type = '【发私信】'
                            }
                            var edit_button = '';
                            if (arguments[i].time_type == 2 && (arguments[i].send_type == 4 || arguments[i].send_type == 3)) {
                                edit_button = '<a href="javascript:void(0)" class="iconfont icon-pen icon-Writing" sina_type="' + arguments[i].sina_type + '" title="编辑"></a><a href="javascript:void(0);" delete-event="" class="glyphicon glyphicon-trash icon-delete" title="删除" weiboid="' + arguments[i].uid + '"></a>'
                            } else if (arguments[i].send_type != 1) {
                                edit_button = '<a href="javascript:void(0);" delete-event="" class="glyphicon glyphicon-trash icon-delete" title="删除" weiboid="' + arguments[i].uid + '"></a>'
                            }

                            _html += '<div class="msg-type1">' +
                                '<div class="msg-head">' +
                                '<span class="send-date">' + arguments[i].time + '</span><span class="send-type" data="">' + time_type + '</span>' +
                                '</div>' +
                                '<div class="msg-body" id="' + arguments[i].id + '">' +
                                '<table class="detail">' +
                                '<tbody>' +
                                '<tr>';
                            if (arguments[i].msgtype != 'text') {
                                _html += '<td class="media"><img src="' + util.imgTimthumb(arguments[i].pic_url, 128, 100) + '"></td>' +
                                    '<td class="title">' +
                                    '<ul>' + biaot_wz + '</ul>' +
                                    '</td>';
                            } else {
                                _html += '<td class="text_title title">' + biaot_wz + '</td>'
                            }

                            _html += '<td class="txt-msgtype">' +
                                '<span>' + sina_type + '</span><br>' +
                                '<span>' + type + '</span>' +
                                '</td>' +
                                '<td class="txt-sendTo">' +
                                '<span>' + visible + '</span>' +
                                '</td>' +
                                '<td class="txt-msgstatus">' + send_type + '</td>' +
                                '<td class="btn-operate">' + edit_button + '</td>' +
                                '</tr>' +
                                '</tbody>' +
                                '</table>' +
                                '</div>' +
                                '</div>';
                        }
                    }
                    $('.weibo_msglist').append(_html);
                    if (param.pag) {
                        $this.paging(totalPage);
                    }
                    if (totalPage > 10) {
                        $("#weibo_paging").show();
                    }
                },
                complete: function() {
                    $('.icon-Writing, .icon-delete', '.msg-content').tooltip({
                        trigger: 'hover',
                        container: 'body'
                    });
                    message.hide();
                }
            })
        },
        //微博页码
        paging: function(argument) {
            $("#weibo_paging").pagination(argument, {
                num_edge_entries: 1,
                num_display_entries: 10,
                items_per_page: 10,
                link_to: 'javascript:;',
                prev_text: '上一页',
                next_text: '下一页',
                callback: function(_index, jq) {
                    if (param.pag == true) {
                        param.pag = false;
                        return;
                    } else {
                        param.sina.data.p = _index + 1;
                        weibo_list.infor_list();
                    }
                }
            });
        },
        //微博时间筛选
        dateFilter: function() {
            var $this = this;
            var dateRange = new pickerDateRange('date-fromto-text2', {
                isTodayValid: true,
                startDate: param[acount.pt].data.time[0],
                endDate: param[acount.pt].data.time[1],
                isSingleDay: false, // 是否单选天
                shortOpr: false, // 是否只选一天
                defaultText: ' 至 ',
                inputTrigger: 'weibo-date-fromto',
                theme: 'ta',
                stopToday: false,
                success: function(obj) {
                    param.sina.data.time = [obj.startDate, obj.endDate];
                    param.sina.data.p = 1;
                    param.pag = true;
                    $this.infor_list();
                }
            });
            return this;
        },
    }
    var tout_list = {
        init: function() {
            if (param.toutiao.timeFilter) {
                this.dateFilter();
                param.toutiao.timeFilter = false;
            }
            this.infor_list();
        },
        //头条消息列表
        infor_list: function() {
            var $this = this;
            message.show('正在加载...');
            $("#toutiao_paging").hide();
            $('.toutiao_msglist').empty();
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: param.toutiao.url,
                data: param.toutiao.data,
                success: function(data) {
                    var arguments = data.data.arts;
                    var totalPage = data.data.total;
                    var _html = '';

                    if (totalPage == 0) {
                        _html = '<div class="no-content"><span class="glyphicon glyphicon-flash"></span>暂无群发消息记录</div>';
                    } else {
                        for (var i in arguments) {
                            var time_type = arguments[i].time_type == 1 ? '立即发送' : '定时发送',
                                type,
                                biaot_wz,
                                send_type;

                            var img_src = null;
                            if (arguments[i].type == 'mpnews') {
                                type = '图文';
                                biaot_wz = '<li><a href="/article?p=' + arguments[i].article_id + '" target="_blank" title="' + (arguments[i].title || '') + '">' + (arguments[i].title || '') + '</a></li>';
                                if (arguments[i].cover != null) {
                                    img_src = '<img src="' + util.imgTimthumb(arguments[i].cover, 128, 100) + '">'
                                } else {
                                    img_src = '<img src="/Public/static/images/wuyulantu.png">'
                                }
                            } else {
                                type = '视频'
                                biaot_wz = '<li>' + (arguments[i].title || '') + '</li>';
                                img_src = '<video class="video-js vjs-default-skin" controls=""><source src="' + arguments[i].content + '"></video>'
                            }
                            if (arguments[i].is_delete == 2) {
                                send_type = '已删除'
                            } else {
                                if (arguments[i].send_type == 2) {
                                    send_type = '发送成功<a href="https://mp.toutiao.com/core/article/index/?source_type=0" target="_blank">查看审核</a>'
                                } else if (arguments[i].send_type == 3) {
                                    send_type = '发送失败'
                                } else {
                                    send_type = '待发送'
                                }
                            }
                            var edit_button = '';

                            if (arguments[i].is_delete != 2) {
                                if (arguments[i].time_type == 2 && (arguments[i].send_type == 1 || arguments[i].send_type == 3)) {
                                    edit_button = '<a href="javascript:void(0)" class="iconfont icon-pen icon-Writing" sina_type="' + arguments[i].sina_type + '" title="编辑"></a><a href="javascript:void(0);" delete-event="" class="glyphicon glyphicon-trash icon-delete" title="删除" toutiaoid="' + arguments[i].uid + '"></a>';
                                } else if (arguments[i].send_type != 2) {
                                    edit_button = '<a href="javascript:void(0);" delete-event="" class="glyphicon glyphicon-trash icon-delete" title="删除" toutiaoid="' + arguments[i].uid + '"></a>';
                                }                                
                            }


                            _html += '<div class="msg-type1"><div class="msg-head">' +
                                '<span class="send-date">' + arguments[i].send_time + '</span><span class="send-type" data="">' + time_type + '</span>' +
                                '</div><div class="msg-body" id="' + arguments[i].id + '">' +
                                '<table class="detail">' +
                                '<tbody>' +
                                '<tr>' +
                                '<td class="media">' + img_src + '</td>' +
                                '<td class="title">' +
                                '<ul>' + biaot_wz + '</ul>' +
                                '</td>' +
                                '<td class="txt-msgtype">' +
                                '<span>' + type + '</span>' +
                                '</td>' +
                                '<td class="txt-sendTo">' +
                                '<span>平台：头条</span><br>' +
                                '<span>账号：' + arguments[i].name + '</span>' +
                                '</td>';

                            if (arguments[i].send_type == 3) {
                                _html += '<td class="txt-msgstatus error_prompt"><div><span class="status">' + send_type + '</span><span class="caret"></span><div class="popup"><ul><li>' + arguments[i].remarks + '</li></ul></div></div></td>';
                            } else {
                                _html += '<td class="txt-msgstatus">' + send_type + '</td>';
                            }
                            _html += '<td class="btn-operate">' + edit_button + '</td>' +
                                '</tr></tbody></table></div></div>'
                        }
                    }
                    $('.toutiao_msglist').append(_html);
                    if (param.pag) {
                        $this.paging(totalPage);
                    }
                    if (totalPage > 10) {
                        $("#toutiao_paging").show();
                    }
                },
                complete: function() {
                    $('.icon-Writing, .icon-delete', '.msg-content').tooltip({
                        trigger: 'hover',
                        container: 'body'
                    });
                    message.hide();
                }
            })
        },
        //头条页码
        paging: function(argument) {
            $("#toutiao_paging").pagination(argument, {
                num_edge_entries: 1,
                num_display_entries: 10,
                items_per_page: 10,
                link_to: 'javascript:;',
                prev_text: '上一页',
                next_text: '下一页',
                callback: function(_index, jq) {
                    if (param.pag == true) {
                        param.pag = false;
                        return;
                    } else {
                        param.toutiao.data.p = _index + 1;
                        tout_list.infor_list();
                    }
                }
            });
        },
        //头条时间筛选
        dateFilter: function() {
            var $this = this;
            var dateRange = new pickerDateRange('date-fromto-text3', {
                isTodayValid: true,
                startDate: param[acount.pt].data.time[0],
                endDate: param[acount.pt].data.time[1],
                isSingleDay: false, // 是否单选天
                shortOpr: false, // 是否只选一天
                defaultText: ' 至 ',
                inputTrigger: 'toutiao-date-fromto',
                theme: 'ta',
                stopToday: false,
                success: function(obj) {
                    param.toutiao.data.time = [obj.startDate, obj.endDate];
                    param.toutiao.data.p = 1;
                    param.pag = true;
                    tout_list.infor_list();
                }
            });
            return this;
        },
    }
    var HtmlBuild = {
        options: {
            //信息类型
            msgType: {
                video: '视频',
                mpnews: "图文",
                image: "图片",
                voice: "语音",
                text: "文字"
            },
            //信息状态
            msgStatus: {
                1: '已发送',
                2: '待发送',
                3: '发送失败',
                4: '已删除',
                10: '发送中',
                11: '发送异常',
                12: '发送失败',
                16: '已发送'
            },
            //信息发送类型
            msgSendType: {
                1: '定时发送',
                0: '立即发送'
            },
            userGender: {
                1: '男',
                2: '女',
                3: '全部'
            }
        },
        //信息头Html
        getMsgHeadHtml: function(obj) {
            return '<div class="msg-type1"><div class="msg-head">' +
                '<span class="send-date">' + obj.appointment_time + '</span>' +
                '<span class="send-type" data="' + obj.is_timer + '">' + this.options.msgSendType[obj.is_timer] + '</span>' +
                '</div>'
        },
        //信息类型html
        getMsgTypeHtml: function(msg_type_cd) {
            return '<td class="txt-msgtype">【' + this.options.msgType[msg_type_cd] + '】</td>';
        },
        //图片Html
        getMsgImgHtml: function(img, obj, cover_img_url) {
            var isrc = '';
            if (obj.msg_type_cd == 'video') {
                isrc = '<td class="media">' +
                    '<video class="video-js vjs-default-skin" controls=""><source src="' + cover_img_url + '"></video>' +
                    '</td><td class="title">';
            } else if (obj.msg_type_cd == 'image' || obj.msg_type_cd == 'mpnews') {
                isrc = '<td class="media">' +
                    '<img src="' + (util.imgTimthumb(img, 129, 100) || './Public/dev/images/wuyulantu.png') + '"/>' +
                    '</td><td class="title">';
            } else if (obj.msg_type_cd == 'voice') {
                isrc = '<td class="media"><div class="voice_player" sound_src="' + cover_img_url + '"><span class="iconfont icon-voice"></span></div><td class="title">';
            } else {
                isrc = '<td class="text_title title">'
            }

            return '<div class="msg-body" id="' + obj.id + '" type="' + obj.is_timer + '" data-id="' + obj.res_id + '">' +
                '<table class="detail">' +
                '<tr>' + isrc;
        },
        //信息标题Html
        getMsgTitleHtml: function(titles, msg_type_cd, cover_img_url) {
            titleMsgHtml = '<ul>';
            if (titles.length == 1) {
                for (var i in titles) {
                    var title = titles[i];
                    if (msg_type_cd == 'text') {
                        titleMsgHtml = '<div>';
                        titleMsgHtml += '<div class="text" title="' + title.title + '">' + title.title + '</div>';
                    }
                    if (msg_type_cd == 'mpnews') {
                        titleMsgHtml += '<li>' + '<a href="/article?p=' + title.cd + '" target="_blank" title="' + title.title + '">' + title.title + '</a></li>';
                    }
                    if (msg_type_cd == 'image') {
                        titleMsgHtml += '<li>' + '<a href="' + cover_img_url + '" target="_blank" title="' + title.title + '">' + title.title + '</a></li>';
                    }
                    if (msg_type_cd == 'video') {
                        titleMsgHtml += '<li>' + '<a href="' + cover_img_url + '" target="_blank" title="' + title.title + '">' + title.title + '</a></li>';
                    }
                    if (msg_type_cd == 'voice') {
                        titleMsgHtml += '<li>' + '<a href="' + cover_img_url + '" target="_blank" title="' + title.title + '">' + title.title + '</a></li><li>' + timeToStr(title.length) + '</li>';
                    }
                }
                if (msg_type_cd == 'text') {
                    titleMsgHtml += '</div>';
                }
                if (msg_type_cd == 'mpnews') {
                    titleMsgHtml += '</ul>';
                }
                if (msg_type_cd == 'image') {
                    titleMsgHtml += '</ul>';
                }
                if (msg_type_cd == 'video') {
                    titleMsgHtml += '</ul>';
                }
            } else {
                for (var i in titles) {
                    var title = titles[i];
                    var num = parseInt(i) + parseInt(1);
                    titleMsgHtml += '<li>' + (num) + '、' + '<a href="/article?p=' + title.cd + '" target="_blank" title="' + title.title + '">' + title.title + '</a></li>';
                }
                titleMsgHtml += '</ul>';
            }
            return titleMsgHtml;
        },
        //信息状态html
        getMsgStatusHtml: function(js_result, msg_status_cd) {
            if (msg_status_cd == 2 || msg_status_cd == 4 || msg_status_cd == 10 || js_result == null) {
                return '<td class="txt-msgstatus">' + this.options.msgStatus[msg_status_cd] + '</td>';
            } else {
                return '<td class="txt-msgstatus" status="' + msg_status_cd + '"><span class="status">' + this.options.msgStatus[msg_status_cd] + '</span><span class="caret">' + '<div class="popup">' +
                    '<ul>' +
                    '<li>' + js_result + '</li>' +
                    '</ul>' +
                    '</div>' + '</span>' + '</td>';
            }
        },
        //发送对象html
        getSendHtml: function(send_to) {
            var sendToHtml = '<td class="txt-sendTo">' +
                '<span>' + '平台:' + send_to.app + '</span>' + '<br />' +
                '<span>' + '账号:' + send_to.app_name + '</span>' + '<br />' +
                '<span>' + '用户:' + send_to.app_user_group + '</span>';
            if (send_to.gender == 1 || send_to.gender == 2) {
                sendToHtml += '<br /><span class="gender">' + '性别:' + this.options.userGender[send_to.gender] + '</span>'
            }
            sendToHtml += '</td>';
            return sendToHtml;
        }
    }

    function timeToStr(time) {
        if (!time) {
            return '';
        }
        var h = 0,
            m = 0,
            s = 0,
            _h = '00',
            _m = '00',
            _s = '00';
        time = parseInt(time);
        h = Math.floor(time / 3600);
        time = Math.floor(time % 3600);
        m = Math.floor(time / 60);
        s = Math.floor(time % 60);
        _s = s < 10 ? '0' + s : s + '';
        _m = m < 10 ? '0' + m : m + '';
        _h = h < 10 ? '0' + h : h + '';
        return _m + ":" + _s;
    }

    //关键字搜索
    $('.search .search_btn').click(function() {
        var sear_key = $('input.search_input').val();
        param[acount.pt].data.p = 1;
        param.pag = true;
        (acount.pt == 'weixin') ? (param[acount.pt].data.key = sear_key) : (param[acount.pt].data.select = sear_key);
        if (acount.pt == 'weixin') {
            weixin_msg._doSearchMsg();
        } else if (acount.pt == 'sina') {
            weibo_list.infor_list();
        } else {
            tout_list.infor_list();
        }
    });

    //按信息类型筛选
    $('.controll_tab').on('change', 'select.msg-type-select', function() {
        param[acount.pt].data.p = 1;
        param.pag = true;
        if (acount.pt == 'weixin') {
            param[acount.pt].data.mt = $(this).val();
            weixin_msg._doSearchMsg();
        } else if (acount.pt == 'sina') {
            param[acount.pt].data.type = $(this).val();
            weibo_list.infor_list();
        } else {
            param[acount.pt].data.type = $(this).val();
            tout_list.infor_list();
        }
    });
    //按信息状态筛选
    $('.controll_tab').on('change', 'select.msg-status-select', function() {
        param[acount.pt].data.p = 1;
        param.pag = true;
        if (acount.pt == 'weixin') {
            param[acount.pt].data.ms = $(this).val();
            weixin_msg._doSearchMsg();
        } else if (acount.pt == 'sina') {
            param[acount.pt].data.send_type = $(this).val();
            weibo_list.infor_list();
        } else {
            param[acount.pt].data.send_type = $(this).val();
            tout_list.infor_list();
        }
    });

    $('.weibo_list').on('change', 'select.send-status-select', function() {
        if ($(this).val() == 'message') {
            $('.weibo_list select.msg-type-select').html('<option value="">全部类型</option>' +
                '<option value="articles">图文</option>' +
                '<option value="text">文字</option>');
        } else {
            $('.weibo_list select.msg-type-select').html('<option value="">全部类型</option>' +
                '<option value="text">文字</option>' +
                '<option value="image">图片</option>');
        }
        $('.weibo_list .msg-type-select').selectpicker('refresh');
        param[acount.pt].data.sina_type = $(this).val();
        param.pag = true;
        param[acount.pt].data.p = 1;
        weibo_list.infor_list();
    });


    var $msgContent = $('.msg-content');
    // 编辑
    $msgContent.on('click', '.btn-operate>.icon-Writing', function() {
        var now_date = new Date().format('yyyy-MM-dd hh:mm:ss').replace(/-/g,"/");
        var time = $(this).parents(".msg-body").siblings(".msg-head").find(".send-date").text().replace(/-/g,"/");
        var _tm = (Date.parse(time) - Date.parse(now_date)) / 1000 / 60;

        if (0 < _tm && _tm < 30) {
            message.showWarning('不能编辑发送时间与当前时间差小于30分钟的群发消息!', message.showTime);
            return;
        }
        
        var mid = $(this).closest('.detail').parent().attr("id");
        window.location.href = 'sending?send_pltfm=' + acount.pt + '&mid=' + mid;
    });

    $msgContent.on('mouseover', '.txt-msgstatus>span', function() {
        $this = $(this);
        $status = $this.closest('.txt-msgstatus').attr('status');

        if ($status == 1) {
            $this.closest('td.txt-msgstatus').children().children('.popup').css('display', 'block');
        }
        if ($status == 3) {
            $this.closest('td.txt-msgstatus').children().children('.popup').css('display', 'block');
        }
        if ($status > 10) {
            $this.closest('td.txt-msgstatus').children().children('.popup').css('display', 'block');
        }
    });
    $msgContent.on('mouseout', '.txt-msgstatus>span', function() {
        $this = $(this);
        $('td.txt-msgstatus').children().children('.popup').css('display', 'none');
    });
    $msgContent.on('click', '.hanssagain', function() {
        var mid = $(this).parents('.msg-body').attr('id');
        var articleTitle = $(this).parents('.msg-body').find('.title ul li:first a').attr('title');
        var app_id = $(this).parents('.msg-body').find('.btn-operate a').attr('data-id');
        var ssta = $(this);
        $.ajax({
            type: 'post',
            url: '/Interact/Wechat/AsynSend/send_again',
            timeout: 1000,
            data: {
                id: mid,
                app_id: app_id,
                title: articleTitle,
            },
            complete: function() {
                message.show('正在重发…', 1500);
                ssta.parents('.detail').find('.txt-msgstatus').html('发送中');
            }
        });
    });
    //删除
    cont.on('click', '[delete-event]', function() {
        var target_id = $(this).closest('.detail').parent().attr("id");

        var params = {};
        params.data = {};

        if (acount.pt == 'weixin') {
            params.url = '/SendingLog/delete_news';
            params.data.lid = target_id;
            params.data.app_id = $(this).attr("data-id");
        } else if (acount.pt == 'sina') {
            params.url = thisAPI.WEIBO_INTERFACE.DELETE_STATUS;
            params.data.id = target_id;
            params.data.wb_id = $(this).attr("weiboid");
        } else {
            params.url = '/toutiao/tt_art_del';
            params.data.id = target_id;
            params.data.uid = $(this).attr("toutiaoid");
        }
        delete_list.deleteConfirmShowBottom({
            obj: this,
            fun: function() {
                deleteTarget(params);
            }
        });
    })

    function deleteTarget(json) {
        var length = '';
        var pt = acount.pt;
        if (pt == 'weinxin') {
            length = cont.find('.weixin_msglist > div.msg-type1').length;
        } else if (pt == 'sina') {
            length = cont.find('.weibo_msglist > div.msg-type1').length;
        } else {
            length = cont.find('.toutiao_msglist > div.msg-type1').length;
        }

        $.ajax({
            type: 'post',
            url: json.url,
            data: json.data,
            success: function(json) {
                var Json = $.parseJSON(json);
                if (Json.msg == 'ok') {
                    message.show('删除成功！');
                    if (length == 0 && param[acount.pt].data.p > 1) {
                        param.pag = true;
                        param[acount.pt].data.p = param[acount.pt].data.p - 1;
                    }
                    if (pt == 'weixin') {
                        weixin_msg._doSearchMsg();
                    } else if (pt == 'sina') {
                        weibo_list.infor_list();
                    } else {
                        tout_list.infor_list();
                    }
                } else {
                    message.show(Json.msg, 4000);
                }
            }
        })
    };
})
